package io.zcoder.innoway.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.adapters.HistoryEntryTimeAdapter;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.models.EntryMonth;
import io.zcoder.innoway.models.EntryTime;
import io.zcoder.innoway.models.EntryTimeResponse;
import io.zcoder.innoway.rest.RestClient;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class TrendHistoryActivity extends BaseActivity implements HistoryEntryTimeAdapter.EntryCallBack {

    @BindView(R.id.common_back)
    ImageView mBack;
    @BindView(R.id.list_history)
    RecyclerView mList;

    @OnClick(R.id.common_back)
    void back() {
        onBackPressed();
    }

    private int mUnit;
    private int mPosition;
    private List<EntryTime> mEntryTimes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_trend_history_activity);
        ButterKnife.bind(this);
        mBack.setVisibility(View.VISIBLE);
        mPosition = getIntent().getIntExtra(Constants.INDEX_EXTRAS, 0);
        mUnit = getIntent().getIntExtra(Constants.UNIT_EXTRAS, 0);
        setTitle(String.format(getString(R.string.history_s), getString(Utils.getIdEntry(mPosition))));

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mList.setLayoutManager(layoutManager);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2016, 1, 1);
        String type = getString(Utils.getIdEntry(mPosition)).toLowerCase().replace(" ", "_");
        RestClient.build(this, true).getEntryTime(PrefUtils.getInt(this, PrefUtils.PREF_PROFILE_ID),
                Utils.convertToString(calendar.getTime(), Utils.DATE_FORMAT_YYY_MM_DD),
                Utils.convertToString(Calendar.getInstance().getTime(), Utils.DATE_FORMAT_YYY_MM_DD), type, "month",
                new Callback<EntryTimeResponse>() {
                    @Override
                    public void success(EntryTimeResponse entryTimeResponse, Response response) {
                        if (entryTimeResponse.getStatus_code() != 200) {
                            showAlert(getString(R.string.error), entryTimeResponse.getMessage());
                        } else {
                            mEntryTimes = entryTimeResponse.getData();
                            String unit = "%";
                            if (mPosition == 0) {
                                unit =  Utils.getUnitWeight(mUnit);
                            }
                            if (mPosition == 6) {
                                unit = "Kg";
                            }
                            if (mPosition == 7) {
                                unit = "Kcal";
                            }
                            HistoryEntryTimeAdapter adapter = new HistoryEntryTimeAdapter(TrendHistoryActivity.this,
                                    mEntryTimes, unit);
                            mList.setAdapter(adapter);
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                    }
                });
    }

    @Override
    public void onSelect(int position) {
        if (mEntryTimes == null) {
            return;
        }
        Intent intent = new Intent(this, TrendHistoryByMonthActivity.class);
        EntryMonth entryMonth = new EntryMonth(mPosition, position, mUnit, mEntryTimes);
        intent.putExtra(Constants.ENTRY_MONTH_EXTRAS, entryMonth);
        startActivity(intent);
    }
}
